import request from '@/utils/request'

/**
 * 设置用户简单信息
 * @param {*} data
 * @returns
 */
export const setUserInfo = (data) => ({
  type: 'profile_setUserInfo',
  payload: data,
})

/**
 * 设置用户详细信息
 * @returns
 */

export const setUserProfile = (data) => ({
  type: 'profile_setUserProfile',
  payload: data,
})

/**
 * 获取用户简易信息
 * @returns
 */
export const getUserInfo = () => async (dispatch) => {
  try {
    const res = await request.get('/user')
    dispatch(setUserInfo(res.data))
  } catch (err) {
    console.log(err)
  }
}

/**
 * 获取用户详细信息
 * @returns
 */
export const getUserProfile = () => async (dispatch) => {
  try {
    const { data: res } = await request.get('/user/profile')
    dispatch(setUserProfile(res))
  } catch (err) {
    console.log(err)
  }
}

/**
 * 更新用户信息
 * @param {*} data
 * @returns
 */
export const editUserProfile = (data) => async (dispatch) => {
  try {
    await request.patch('/user/profile', data)
    dispatch(getUserProfile())
  } catch (err) {
    console.log(err)
  }
}

/**
 * 更新头像
 * @param {*} formData
 * @returns
 */
export const editUserPhoto = (formData) => async (dispatch) => {
  try {
    await request.patch('/user/photo', formData)
    dispatch(getUserProfile())
  } catch (err) {
    console.log(err)
  }
}
